﻿using Mapster;
using Raffles.OA.Data;
using Raffles.OA.Service;
using Raffles.OA.Utility;
using Raffles.OA.WebApp.App_Filter;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Sale.Controllers
{
    /// <summary>
    /// 日 期：2024-07-15 16:51:54
    /// 描 述：信息中心
    /// </summary>I
    [Login]
    public class BaseInfoController : Controller
    {
        private static SqlSugarScope _dbContext = ReportDB.Db;
        private BaseItemService _baseItemService;
        

        public BaseInfoController()
        {
            _baseItemService = new BaseItemService();
        }

        #region 视图

        public virtual ActionResult Index()
        {
            return View();
        }

        public ActionResult BaseForm()
        {
            return View();
        }

        /// <summary>
        /// 团组培训
        /// </summary>
        /// <returns></returns>
        public ActionResult BaseActivityForm()
        {
            return View();
        }

        #endregion 视图

        /// <summary>
        /// 报价类目
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetQuotationItemJson()
        {
            try
            {
                var data = await _dbContext.Queryable<BaseQuotationItemEntity>()
                                           .ToListAsync();
                return Content(DataResult<List<BaseQuotationItemEntity>>.Success(data, "加载成功").ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 行程城市
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetDestinationJson()
        {
            try
            {
                var data = await _dbContext.Queryable<BaseDestinationEntity>()
                                           .Where(x => !x.DeletedMark)
                                           .ToListAsync();
                return Content(DataResult<List<BaseDestinationEntity>>.Success(data, "加载成功").ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 酒店住宿
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetHotelJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseHotelEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseHotelEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 团队餐饮
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetRestaurantJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseRestaurantEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseRestaurantEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 交通导游
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetTransportationJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseTransportationEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseTransportationEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 景点门票
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetScenicAreaJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseScenicAreaEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseScenicAreaEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 团组公务
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetGroupOfficialJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseGroupOfficialEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseGroupOfficialEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 团组培训
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetGroupActivityJson()
        {
            try
            {
                var data = await _dbContext.Queryable<BaseGroupActivityEntity>()
                                           .Where(x => !x.DeletedMark)
                                           .ToListAsync();
                return Content(DataResult<List<BaseGroupActivityEntity>>.Success(data, "加载成功").ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 其他费用
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetAdditionJson(Pagination pagination)
        {
            try
            {
                RefAsync<int> totalCount = 0;
                var data = await _dbContext.Queryable<BaseAdditionEntity>()
                                         .Where(x => !x.DeletedMark)
                                         .ToPageListAsync(pagination.PageIndex, pagination.PageSize, totalCount);
                return Content(DataResult<List<BaseAdditionEntity>>.PageResult(data, totalCount).ToJson());
            }
            catch (Exception ex)
            {
                return Content(DataResult.Error($"加载错误,错误原因:{ex.Message}").ToJson());
            }
        }

        /// <summary>
        /// 获取行程城市下拉树
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetDestinationTreeSelect()
        {
            List<TreeVo> treeDtos = new List<TreeVo>();
            try
            {
                treeDtos = await _baseItemService.GetDestinationTree();

                return Json(DataResult<List<TreeVo>>.Success(treeDtos, "加载成功"), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"加载错误,错误原因:{ex.Message}"), JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 保存基础信息
        /// </summary>
        /// <param name="baseItemDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> SaveBaseForm(BaseItemDto baseItemDto)
        {
            try
            { 
                int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(baseItemDto.Code);
                switch (enumValue)
                {
                    #region 报价类目

                    //团队住宿
                    case (int)QuotationItemEnum.Hotel:
                        BaseHotelEntity hotelEntity = new BaseHotelEntity();
                        baseItemDto.Adapt(hotelEntity);
                        if (baseItemDto.Id == null)
                        {
                            hotelEntity.CreatedBy = hotelEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(hotelEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            hotelEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(hotelEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //团队餐饮
                    case (int)QuotationItemEnum.Restaurant:
                        BaseRestaurantEntity baseRestaurantEntity = new BaseRestaurantEntity();
                        baseItemDto.Adapt(baseRestaurantEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseRestaurantEntity.CreatedBy = baseRestaurantEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseRestaurantEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseRestaurantEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseRestaurantEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //交通导游
                    case (int)QuotationItemEnum.Transportation:
                        BaseTransportationEntity baseTransportationEntity = new BaseTransportationEntity();
                        baseItemDto.Adapt(baseTransportationEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseTransportationEntity.CreatedBy = baseTransportationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseTransportationEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseTransportationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseTransportationEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //景点门票
                    case (int)QuotationItemEnum.ScenicArea:
                        BaseScenicAreaEntity baseScenicAreaEntity = new BaseScenicAreaEntity();
                        baseItemDto.Adapt(baseScenicAreaEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseScenicAreaEntity.CreatedBy = baseScenicAreaEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseScenicAreaEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseScenicAreaEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseScenicAreaEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //团组公务
                    case (int)QuotationItemEnum.GroupOfficial:
                        BaseGroupOfficialEntity baseGroupOfficialEntity = new BaseGroupOfficialEntity();
                        baseItemDto.Adapt(baseGroupOfficialEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseGroupOfficialEntity.CreatedBy = baseGroupOfficialEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseGroupOfficialEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseGroupOfficialEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseGroupOfficialEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //团组培训
                    case (int)QuotationItemEnum.GroupActivity:
                        BaseGroupActivityEntity baseGroupActivityEntity = new BaseGroupActivityEntity();
                        baseItemDto.Adapt(baseGroupActivityEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseGroupActivityEntity.CreatedBy = baseGroupActivityEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseGroupActivityEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseGroupActivityEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseGroupActivityEntity).ExecuteCommandAsync();
                        } 

                        break;
                    //其他费用
                    case (int)QuotationItemEnum.Addition:
                        BaseAdditionEntity baseAdditionEntity = new BaseAdditionEntity();
                        baseItemDto.Adapt(baseAdditionEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseAdditionEntity.CreatedBy = baseAdditionEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseAdditionEntity).ExecuteCommandIdentityIntoEntityAsync();
                        }
                        else
                        {
                            baseAdditionEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseAdditionEntity).ExecuteCommandAsync();
                        } 

                        break;

                    #endregion 报价类目

                    #region 报价数据

                    //行程城市
                    case (int)QuotationItemEnum.Destination:
                        BaseDestinationEntity baseDestinationEntity = new BaseDestinationEntity();
                        baseItemDto.Adapt(baseDestinationEntity);
                        if (baseItemDto.Id == null)
                        {
                            baseDestinationEntity.CreatedBy = baseDestinationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Insertable(baseDestinationEntity).ExecuteCommandAsync();
                        }
                        else
                        {
                            baseDestinationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                            await _dbContext.Updateable(baseDestinationEntity).ExecuteCommandAsync();
                        }
                        await _baseItemService.LoadQuotationDestinationCache();
                        break;

                    #endregion 报价数据

                    default:
                        break;
                }
                await _baseItemService.LoadQuotationRelatePriceCache();
                return Json(DataResult.Success($"操作成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 编辑基础信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ActionResult> SetBaseForm(int id, string code)
        {
            int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(code);
            switch (enumValue)
            {
                #region 报价类目

                //团队住宿
                case (int)QuotationItemEnum.Hotel:
                    BaseHotelEntity hotelEntity = await _dbContext.Queryable<BaseHotelEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseHotelEntity>.Success(hotelEntity).ToJson());
                //团队餐饮
                case (int)QuotationItemEnum.Restaurant:
                    BaseRestaurantEntity baseRestaurantEntity = await _dbContext.Queryable<BaseRestaurantEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseRestaurantEntity>.Success(baseRestaurantEntity).ToJson());
                //交通导游
                case (int)QuotationItemEnum.Transportation:
                    BaseTransportationEntity baseTransportationEntity = await _dbContext.Queryable<BaseTransportationEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseTransportationEntity>.Success(baseTransportationEntity).ToJson());
                //景点门票
                case (int)QuotationItemEnum.ScenicArea:
                    BaseScenicAreaEntity baseScenicAreaEntity = await _dbContext.Queryable<BaseScenicAreaEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseScenicAreaEntity>.Success(baseScenicAreaEntity).ToJson());
                //团组公务
                case (int)QuotationItemEnum.GroupOfficial:
                    BaseGroupOfficialEntity baseGroupOfficialEntity = await _dbContext.Queryable<BaseGroupOfficialEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseGroupOfficialEntity>.Success(baseGroupOfficialEntity).ToJson());
                //团组培训
                case (int)QuotationItemEnum.GroupActivity:
                    BaseGroupActivityEntity baseGroupActivityEntity = await _dbContext.Queryable<BaseGroupActivityEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseGroupActivityEntity>.Success(baseGroupActivityEntity).ToJson());
                //其他费用
                case (int)QuotationItemEnum.Addition:
                    BaseAdditionEntity baseAdditionEntity = await _dbContext.Queryable<BaseAdditionEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseAdditionEntity>.Success(baseAdditionEntity).ToJson());

                #endregion 报价类目

                #region 报价数据

                //行程城市
                case (int)QuotationItemEnum.Destination:
                    BaseDestinationEntity baseDestinationEntity = await _dbContext.Queryable<BaseDestinationEntity>().InSingleAsync(id);
                    return Content(DataResult<BaseDestinationEntity>.Success(baseDestinationEntity).ToJson());

                #endregion 报价数据

                default:
                    break;
            }
            return Content(DataResult.Success().ToJson());
        }

        /// <summary>
        /// 保存团组培训明细项
        /// </summary>
        /// <param name="baseItemDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> SaveBaseActivityDeatilForm(BaseActivityDeatilDto baseActivityDeatilDto)
        {
            try
            {
                await _dbContext.BeginTranAsync();

                //删除历史明细
                await _dbContext.Deleteable<BaseGroupActivityDetailEntity>().Where(x => x.ActivityId == baseActivityDeatilDto.ActivityId).ExecuteCommandAsync();

                //插入当前明细
                List<BaseGroupActivityDetailEntity> entitys = baseActivityDeatilDto.ActivityDeatil.Adapt<List<BaseGroupActivityDetailEntity>>();
                await _dbContext.Insertable<BaseGroupActivityDetailEntity>(entitys).ExecuteCommandAsync();

                //更新表头基础单价合计
                BaseGroupActivityEntity baseGroupActivityEntity = await _dbContext.Queryable<BaseGroupActivityEntity>().InSingleAsync(baseActivityDeatilDto.ActivityId);
                baseGroupActivityEntity.Price = entitys.Sum(x => x.ItemSalePrice);
                await _dbContext.Updateable<BaseGroupActivityEntity>(baseGroupActivityEntity).UpdateColumns(x => x.Price).ExecuteCommandAsync();

                await _baseItemService.LoadQuotationRelatePriceCache();

                await _dbContext.CommitTranAsync();
                return Json(DataResult.Success("保存成功"));
            }
            catch (Exception ex)
            {
                await _dbContext.RollbackTranAsync();
                return Json(DataResult.Error($"{ex.Message}"));
            }
        }

        /// <summary>
        /// 编辑团组培训明细项
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public async Task<ActionResult> SetBaseActivityDeatilForm(int activityId)
        {
            List<BaseGroupActivityDetailEntity> entitys = await _dbContext.Queryable<BaseGroupActivityDetailEntity>().Where(x => x.ActivityId == activityId).ToListAsync();
            return Content(DataResult<List<BaseGroupActivityDetailEntity>>.Success(entitys).ToJson());
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteBaseForm(string ids, string code)
        {
            try
            {
                await _dbContext.BeginTranAsync();

                int[] idsArr = ids.SpitIntArrary();
                int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(code);
                switch (enumValue)
                {
                    #region 报价类目

                    //团队住宿
                    case (int)QuotationItemEnum.Hotel:
                        await _dbContext.Updateable<BaseHotelEntity>().SetColumns(x => new BaseHotelEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //团队餐饮
                    case (int)QuotationItemEnum.Restaurant:
                        await _dbContext.Updateable<BaseRestaurantEntity>().SetColumns(x => new BaseRestaurantEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //交通导游
                    case (int)QuotationItemEnum.Transportation:
                        await _dbContext.Updateable<BaseTransportationEntity>().SetColumns(x => new BaseTransportationEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //景点门票
                    case (int)QuotationItemEnum.ScenicArea:
                        await _dbContext.Updateable<BaseScenicAreaEntity>().SetColumns(x => new BaseScenicAreaEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                 .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //团组公务
                    case (int)QuotationItemEnum.GroupOfficial:
                        await _dbContext.Updateable<BaseGroupOfficialEntity>().SetColumns(x => new BaseGroupOfficialEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //团组培训
                    case (int)QuotationItemEnum.GroupActivity:
                        await _dbContext.Updateable<BaseGroupActivityDetailEntity>().Where(x => idsArr.Contains(x.ActivityId)).ExecuteCommandAsync();
                        await _dbContext.Updateable<BaseGroupActivityEntity>().SetColumns(x => new BaseGroupActivityEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;
                    //其他费用
                    case (int)QuotationItemEnum.Addition:
                        await _dbContext.Updateable<BaseAdditionEntity>().SetColumns(x => new BaseAdditionEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                     .Where(x => idsArr.Contains(x.Id)).ExecuteCommandAsync();
                        break;

                    #endregion 报价类目

                    #region 报价数据

                    //行程城市
                    case (int)QuotationItemEnum.Destination:
                        await _dbContext.Updateable<BaseDestinationEntity>().SetColumns(x => new BaseDestinationEntity { DeletedMark = true, UpdatedBy = OperatorProvider.Provider.GetCurrent().Id, UpdatedOn = DateTime.Now })
                                                                    .Where(x => idsArr.Contains(x.Id) || idsArr.Contains(x.ParentId)).ExecuteCommandAsync();

                        await _baseItemService.LoadQuotationDestinationCache();
                        break;

                    #endregion 报价数据

                    default:
                        break;
                }

                await _baseItemService.LoadQuotationRelatePriceCache();

                await _dbContext.CommitTranAsync();

                return Json(DataResult.Success("删除成功"));
            }
            catch (Exception ex)
            {
                await _dbContext.RollbackTranAsync();
                return Json(DataResult.Error($"{ex.Message}"));
            }
        }

        /// <summary>
        /// 状态改变
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> ChangeStatus(int id, string code)
        {
            int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(code);
            var result = false;
            switch (enumValue)
            {
                #region 报价类目

                //团队住宿
                case (int)QuotationItemEnum.Hotel:
                    BaseHotelEntity hotelEntity = await _dbContext.Queryable<BaseHotelEntity>().InSingleAsync(id);
                    result = hotelEntity.EnabledMark;
                    hotelEntity.EnabledMark = !hotelEntity.EnabledMark;
                    hotelEntity.UpdatedOn = DateTime.Now;
                    hotelEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(hotelEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //团队餐饮
                case (int)QuotationItemEnum.Restaurant:
                    BaseRestaurantEntity baseRestaurantEntity = await _dbContext.Queryable<BaseRestaurantEntity>().InSingleAsync(id);
                    result = baseRestaurantEntity.EnabledMark;
                    baseRestaurantEntity.EnabledMark = !baseRestaurantEntity.EnabledMark;
                    baseRestaurantEntity.UpdatedOn = DateTime.Now;
                    baseRestaurantEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseRestaurantEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //交通导游
                case (int)QuotationItemEnum.Transportation:
                    BaseTransportationEntity baseTransportationEntity = await _dbContext.Queryable<BaseTransportationEntity>().InSingleAsync(id);
                    result = baseTransportationEntity.EnabledMark;
                    baseTransportationEntity.EnabledMark = !baseTransportationEntity.EnabledMark;
                    baseTransportationEntity.UpdatedOn = DateTime.Now;
                    baseTransportationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseTransportationEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //景点门票
                case (int)QuotationItemEnum.ScenicArea:
                    BaseScenicAreaEntity baseScenicAreaEntity = await _dbContext.Queryable<BaseScenicAreaEntity>().InSingleAsync(id);
                    result = baseScenicAreaEntity.EnabledMark;
                    baseScenicAreaEntity.EnabledMark = !baseScenicAreaEntity.EnabledMark;
                    baseScenicAreaEntity.UpdatedOn = DateTime.Now;
                    baseScenicAreaEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseScenicAreaEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //团组公务
                case (int)QuotationItemEnum.GroupOfficial:
                    BaseGroupOfficialEntity baseGroupOfficialEntity = await _dbContext.Queryable<BaseGroupOfficialEntity>().InSingleAsync(id);
                    result = baseGroupOfficialEntity.EnabledMark;
                    baseGroupOfficialEntity.EnabledMark = !baseGroupOfficialEntity.EnabledMark;
                    baseGroupOfficialEntity.UpdatedOn = DateTime.Now;
                    baseGroupOfficialEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseGroupOfficialEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //团组培训
                case (int)QuotationItemEnum.GroupActivity:
                    BaseGroupActivityEntity baseGroupActivityEntity = await _dbContext.Queryable<BaseGroupActivityEntity>().InSingleAsync(id);
                    result = baseGroupActivityEntity.EnabledMark;
                    baseGroupActivityEntity.EnabledMark = !baseGroupActivityEntity.EnabledMark;
                    baseGroupActivityEntity.UpdatedOn = DateTime.Now;
                    baseGroupActivityEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseGroupActivityEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;
                //其他费用
                case (int)QuotationItemEnum.Addition:
                    BaseAdditionEntity baseAdditionEntity = await _dbContext.Queryable<BaseAdditionEntity>().InSingleAsync(id);
                    result = baseAdditionEntity.EnabledMark;
                    baseAdditionEntity.EnabledMark = !baseAdditionEntity.EnabledMark;
                    baseAdditionEntity.UpdatedOn = DateTime.Now;
                    baseAdditionEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseAdditionEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    break;

                #endregion 报价类目

                #region 报价数据

                //行程城市
                case (int)QuotationItemEnum.Destination:
                    BaseDestinationEntity baseDestinationEntity = await _dbContext.Queryable<BaseDestinationEntity>().InSingleAsync(id);
                    result = baseDestinationEntity.EnabledMark;
                    baseDestinationEntity.EnabledMark = !baseDestinationEntity.EnabledMark;
                    baseDestinationEntity.UpdatedOn = DateTime.Now;
                    baseDestinationEntity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    await _dbContext.Updateable(baseDestinationEntity).UpdateColumns(x => new { x.EnabledMark, x.UpdatedOn, x.UpdatedBy }).ExecuteCommandAsync();
                    await _baseItemService.LoadQuotationDestinationCache();
                    break;

                #endregion 报价数据

                default:
                    break;
            }
            await _baseItemService.LoadQuotationRelatePriceCache();
            return result == true ? Json(DataResult.Success("禁用成功")) : Json(DataResult.Success("启用成功"));
        }

        /// <summary>
        /// 获取渠道列表
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetProjectSegmentSelect()
        {
            List<Dictionary<string, object>> dic = await _baseItemService.GetProjectSegmentSelect();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dic).ToJson());
        }

        /// <summary>
        /// 获取报价类目明细项(增加行时的下拉项)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ActionResult> GetProjectQuotationItem(string code)
        {
            int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(code);
            List<ProjectQuotationItemDto> dic = new List<ProjectQuotationItemDto>();
            switch (enumValue)
            {
                //团队住宿
                case (int)QuotationItemEnum.Hotel:
                    dic = await _dbContext.Queryable<BaseHotelEntity>()
                                          .Where(x => !x.DeletedMark)
                                          .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = $"{x.Star}星级-{x.Name}", IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //团队餐饮
                case (int)QuotationItemEnum.Restaurant:
                    dic = await _dbContext.Queryable<BaseRestaurantEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //交通导游
                case (int)QuotationItemEnum.Transportation:
                    dic = await _dbContext.Queryable<BaseTransportationEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //景点门票
                case (int)QuotationItemEnum.ScenicArea:
                    dic = await _dbContext.Queryable<BaseScenicAreaEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //团组公务
                case (int)QuotationItemEnum.GroupOfficial:
                    dic = await _dbContext.Queryable<BaseGroupOfficialEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //团组培训
                case (int)QuotationItemEnum.GroupActivity:
                    dic = await _dbContext.Queryable<BaseGroupActivityEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
                //其他费用
                case (int)QuotationItemEnum.Addition:
                    dic = await _dbContext.Queryable<BaseAdditionEntity>()
                                        .Where(x => !x.DeletedMark)
                                        .Select(x => new ProjectQuotationItemDto { Key = x.Id, Value = x.Name, IsDefault = x.IsDefault }).ToListAsync();
                    break;
            }
            return Content(DataResult<List<ProjectQuotationItemDto>>.Success(dic).ToJson());
        }

        /// <summary>
        /// 获取报价类目明细项基础单价值
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ActionResult> GetBaseItemPrice(int? id, string code)
        {
            int enumValue = EnumHelper.ConvertEnumToInt<QuotationItemEnum>(code);
            decimal? price = 0;
            List<RelatePriceDto> relatePrices = _baseItemService.GetQuotationRelatePriceCache() ?? new List<RelatePriceDto>();
            switch (enumValue)
            {
                //团队住宿
                case (int)QuotationItemEnum.Hotel:
                    var hotelPrice= relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if(hotelPrice!= null)
                    {
                        price = hotelPrice.Price;
                    }
                    else
                    {
                        BaseHotelEntity hotelEntity = await _dbContext.Queryable<BaseHotelEntity>().InSingleAsync(id);
                        price = hotelEntity?.Price; 
                    } 
                    break;
                //团队餐饮
                case (int)QuotationItemEnum.Restaurant:
                    var restaurantPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (restaurantPrice != null)
                    {
                        price = restaurantPrice.Price;
                    }
                    else
                    {
                        BaseRestaurantEntity restaurantEntity = await _dbContext.Queryable<BaseRestaurantEntity>().InSingleAsync(id);
                        price = restaurantEntity?.Price; 
                    } 
                    break;
                //交通导游
                case (int)QuotationItemEnum.Transportation:
                    var transportationPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (transportationPrice != null)
                    {
                        price = transportationPrice.Price;
                    }
                    else
                    {
                        BaseTransportationEntity transportationEntity = await _dbContext.Queryable<BaseTransportationEntity>().InSingleAsync(id);
                        price = transportationEntity?.Price;
                    }
                   
                    break;
                //景点门票
                case (int)QuotationItemEnum.ScenicArea:
                    var scenicAreaPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (scenicAreaPrice != null)
                    {
                        price = scenicAreaPrice.Price;
                    }
                    else
                    {
                        BaseScenicAreaEntity scenicAreaEntity = await _dbContext.Queryable<BaseScenicAreaEntity>().InSingleAsync(id);
                        price = scenicAreaEntity?.Price;
                    }
                   
                    break;
                //团组公务
                case (int)QuotationItemEnum.GroupOfficial:
                    var groupOfficialPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (groupOfficialPrice != null)
                    {
                        price = groupOfficialPrice.Price;
                    }
                    else
                    {
                        BaseGroupOfficialEntity groupOfficialEntity = await _dbContext.Queryable<BaseGroupOfficialEntity>().InSingleAsync(id);
                        price = groupOfficialEntity?.Price;
                    }
                    
                    break;
                //团组培训
                case (int)QuotationItemEnum.GroupActivity:
                    var groupActivityPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (groupActivityPrice != null)
                    {
                        price = groupActivityPrice.Price;
                    }
                    else
                    {
                        BaseGroupActivityEntity groupActivityEntity = await _dbContext.Queryable<BaseGroupActivityEntity>().InSingleAsync(id);
                        price = groupActivityEntity?.Price;
                    }
                    
                    break;
                //其他费用
                case (int)QuotationItemEnum.Addition:
                    var additionPrice = relatePrices.FirstOrDefault(x => x.Code == code && x.ItemId == id);
                    if (additionPrice != null)
                    {
                        price = additionPrice.Price;
                    }
                    else
                    {
                        BaseAdditionEntity additionEntity = await _dbContext.Queryable<BaseAdditionEntity>().InSingleAsync(id);
                        price = additionEntity?.Price;
                    } 
                    break;
            }
            return Content(DataResult<decimal?>.Success(price.ToDecimal()).ToJson());
        }

        /// <summary>
        /// 获取团组培训明细基础值
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> GetBaseActivityDetail(int? id)
        {
            var baseActivityItemDeatil = await _dbContext.Queryable<BaseGroupActivityDetailEntity>()
                                                        .Where(x => x.ActivityId == id)
                                                        .Select(x => new BaseActivityItemDeatil
                                                        {
                                                            ItemName = x.ItemName,
                                                            ItemCostName = "",
                                                            ItemCostPrice = x.ItemCostPrice,
                                                            ItemSalePrice = x.ItemSalePrice,
                                                            ItemQty = x.ItemQty,
                                                            Mark = x.Mark
                                                        })
                                                        .ToListAsync();
            return Content(DataResult<List<BaseActivityItemDeatil>>.Success(baseActivityItemDeatil).ToJson());
        } 
    }
}